//
//  HLSubjectViewController.m
//  HLAerospace
//
//  Created by hulian-ios on 17/8/29.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLSubjectViewController.h"
#import "WRNavigationBar.h"
#import "HLMenuButton.h"
#import "HLSearchViewController.h"
#import "MBProgressHUD+HL.h"
#import "HttpRequestManager.h"
#import <MJExtension/MJExtension.h>
#import "HLCourseRecommand.h"
#import "HLCourse.h"
#import "HLItemCourseView.h"
#import "HLSeeMoreViewController.h"
#import "HLCourseFoldViewController.h"
#import "HLMoreCourse.h"
#import "HLFoldCourse.h"
#import "HLCourseDetailViewController.h"
#import "HLArticleViewController.h"
#import "HLCourseFoldViewController.h"
#import "HLSubjectSectionView.h"
#import "HLCategoryCourseController.h"

@interface HLSubjectViewController () <UITableViewDelegate,UITableViewDataSource,HLSubjectSectionViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *searchButton;

/** 数据源  */
@property (nonatomic ,strong) NSMutableArray *dataArrayList;


@end

@implementation HLSubjectViewController

#pragma mark - Lazy
- (NSMutableArray *)dataArrayList
{
    if (!_dataArrayList) {
        _dataArrayList = [NSMutableArray array];
    }
    return _dataArrayList;
}


#pragma mark - UI

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setUI];
    
    [self loadDataSource];
    
}

- (void)setUI
{
    self.navigationItem.title = @"全部课程";
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    
    [self setupNavItems];
}

- (void)setupNavItems
{
    self.searchButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [_searchButton setBackgroundImage:[UIImage imageNamed:@"搜索按钮"] forState:UIControlStateNormal];
    [_searchButton addTarget:self action:@selector(onClickSearchBtn) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.searchButton];
}

#pragma mark - Request

- (NSArray *)meunTitles
{
    return @[@"航天器系统",@"航天运输系统",@"航天测控系统",@"航天着陆场系统",@"空间武器系统",@"航天应用系统",@"航天发射系统",@"航天指挥控制系统"];
}

- (void)loadDataSource
{
    [MBProgressHUD show];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sign"] = [kHLRequestGetAll signStr];
    __weak typeof(self) weakSelf = self;
    [[HTTPRequestManager defaultManager] HTTPGetRequestWithApiName:kHLRequestGetAll parameters:params success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject) {
        [MBProgressHUD hideHUD];
        if ([responseObject[@"code"] integerValue] == 200) {
            for (NSString *title in [self meunTitles]) {
                NSMutableArray *tempArray = [NSMutableArray array];
                NSArray *tempA =  [HLCourseRecommand mj_objectArrayWithKeyValuesArray:responseObject[@"data"][title]];
                [tempArray addObjectsFromArray:tempA];
                [weakSelf.dataArrayList addObject:tempArray];
            }
             [weakSelf.tableView reloadData];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showErrorMessage:@"网络超时"];
    }];
}


#pragma mark - Private

#pragma mark - tableview delegate / dataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 12: kMainTitleFontSize];
        cell.textLabel.textColor = HLRGB(60, 60, 60);
    }
    NSArray *temp =  self.dataArrayList[indexPath.section];
    if (temp.count > 0) {
        HLCourseRecommand *course = temp[indexPath.row];
        cell.textLabel.text = course.course_name ;
    }
    cell.backgroundColor = HLRGB(245, 245, 245);
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  self.dataArrayList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *temp = self.dataArrayList[section];
    
    return temp.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HLSubjectSectionView *view = [HLSubjectSectionView new];
    view.contentTitle = [self meunTitles][section];
    view.delegate = self;
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001f;
}

#pragma mark - Control


- (void)onClickLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)onClickSearchBtn
{
    HLSearchViewController *search = [[HLSearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}

#pragma mark - HLSubjectSectionViewDelegate
- (void)subjectSectionView:(HLSubjectSectionView *)subjectSectionView didSelectedTitle:(NSString *)title
{
    NSInteger index = [[self meunTitles]indexOfObject:title];
    HLCategoryCourseController *cate = [HLCategoryCourseController new];
    cate.index = index + 1;
    [self.navigationController pushViewController:cate animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *temp = self.dataArrayList[indexPath.section];
    HLCourseRecommand *course = temp[indexPath.row];
    
    HLFoldCourse *foldCourse = [HLFoldCourse new];
    if (course.course_type == 2 ) {
        foldCourse.wkid = [NSString stringWithFormat:@"%ld",course.course_id];
        HLArticleViewController *article = [HLArticleViewController new];
        article.foldCourse = foldCourse;
        article.history_record_pic_url = course.course_pic;
        [self.navigationController pushViewController:article animated:YES];
    }
    
    if (course.course_type == 1) {
        foldCourse.wkid = [NSString stringWithFormat:@"%ld",course.course_id];
        HLCourseDetailViewController *couseDetail = [HLCourseDetailViewController new];
        couseDetail.foldCourse = foldCourse;
        couseDetail.history_record_pic_url = course.course_pic;
        [self.navigationController pushViewController:couseDetail animated:YES];
    }
}

@end
